package com.pachong.frameworkdemo.app;


import android.app.Application;
import android.app.Service;
import android.content.Context;
import android.os.Vibrator;
import android.util.DisplayMetrics;

import com.baidu.mapapi.SDKInitializer;
import com.pachong.android.framework.SecretCodeActivity;
import com.pachong.android.frameworkbase.utils.DimensionUtil;
import com.pachong.baidumaplib.location.service.LocationService;
import com.pachong.baidumaplib.location.service.WriteLog;
import com.pachong.frameworkdemo.BuildConfig;
import com.pachong.frameworkdemo.message.dao.DaoMaster;
import com.pachong.frameworkdemo.message.dao.DaoSession;
import com.uuzuche.lib_zxing.DisplayUtil;

public class MyApplication extends Application {

    private static Context mAppContext = null;

    @Override
    public void onCreate() {
        super.onCreate();
        mAppContext = this.getApplicationContext();
        DimensionUtil.init(mAppContext, getResources());
//        UIL.init(this);
        initBaiduMap();
        SecretCodeActivity.setBuildType(BuildConfig.BUILD_TYPE);

        /**
         * 初始化尺寸工具类
         */
        initDisplayOpinion();
    }

    private void initDisplayOpinion() {
        DisplayMetrics dm = getResources().getDisplayMetrics();
        DisplayUtil.density = dm.density;
        DisplayUtil.densityDPI = dm.densityDpi;
        DisplayUtil.screenWidthPx = dm.widthPixels;
        DisplayUtil.screenhightPx = dm.heightPixels;
        DisplayUtil.screenWidthDip = DisplayUtil.px2dip(getApplicationContext(), dm.widthPixels);
        DisplayUtil.screenHightDip = DisplayUtil.px2dip(getApplicationContext(), dm.heightPixels);
    }

    private void initBaiduMap() {
        LocationService locationService = new LocationService(getAppContext());
        Vibrator vibrator = (Vibrator) getApplicationContext().getSystemService(Service.VIBRATOR_SERVICE);
        WriteLog.getInstance().init(); // 初始化日志
        SDKInitializer.initialize(getApplicationContext());
    }

    public static Context getAppContext() {
        return mAppContext;
    }


    private static DaoMaster mDaoMaster;
    private static DaoSession mDaoSession;
    public static final String DATABASE_NAME = "demoapp.db";

    public static DaoMaster getDaoMaster(Context context) {
        if (mDaoMaster == null) {
            DaoMaster.OpenHelper helper = new DaoMaster.DevOpenHelper(context, DATABASE_NAME, null);
            mDaoMaster = new DaoMaster(helper.getWritableDatabase());
        }
        return mDaoMaster;
    }


    /**
     * 取得DaoSession
     *
     * @param context
     * @return
     */
    public static DaoSession getDaoSession(Context context) {
        if (mDaoSession == null) {
            if (mDaoMaster == null) {
                mDaoMaster = getDaoMaster(context);
            }
            mDaoSession = mDaoMaster.newSession();
        }
        return mDaoSession;
    }


}
